import com.hankcs.hanlp.HanLP;
import com.hankcs.hanlp.seg.common.Term;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;
import org.bson.types.ObjectId;

import java.io.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        MongoCollection<Document> sourse = new MongoClient("192.168.1.174", 27017).getDatabase("LuJingWangDate").getCollection("Yejishuominghui2");
//        FindIterable<Document> documents = sourse.find(new Document("posNum",null));
        FindIterable<Document> documents = sourse.find(new Document("_id",new ObjectId("5b34b3acff88002d90452763")));
        HashSet<String> positiveSet = Test.ReadWord("poative.txt");
        HashSet<String> negativeSet = Test.ReadWord("negative.txt");
        int count = 0;
        for (Document document:documents) {
            String huidarenneirong = document.getString("huidarenneirong");
            System.out.println(document.get("_id").toString()+"\t"+count++);
            if(huidarenneirong != null){
                int posNum = CountWord(huidarenneirong,positiveSet);
                int negNum = CountWord(huidarenneirong,negativeSet);
                document.append("posNum",posNum);
                document.append("negNum",negNum);
                StringBuilder resultBuilder = new StringBuilder();
                List<Term> termList = HanLP.segment(huidarenneirong);
                for(Term term:termList){
                    if(!StopWords.contains(term.word)){
                        resultBuilder.append(term.word+" ");
                    }
                }
                document.append("fenci",resultBuilder.toString());
                int total = resultBuilder.toString().split(" ").length;
                float pospct = (float)posNum/total;
                float negpct = (float)negNum/total;
                if((pospct+negpct)>0){
                    float tone = (pospct-negpct)/(pospct+negpct);
                    document.append("TONE",tone);
                }
                document.append("POSPCT",pospct);
                document.append("NEGPCT",negpct);
                sourse.updateOne(new Document("_id", document.get("_id")), new Document("$set", document),new UpdateOptions().upsert(false));
            }
        }
    }
    public static int CountWord(String line, HashSet<String> set){
        Iterator<String> iter = set.iterator();
        int total = 0;
        while (iter.hasNext()){
            String word = iter.next();
            if(line.contains(word)){
                total = total+WordNum(word,line);
            }
        }
//        System.out.println("--------------");
        return total;
    }
    public static int WordNum(String word, String line){
        int len1 = line.length();
        line = line.replace(word,"");
        int len2 = line.length();
//        System.out.println(len1-len2);
//        System.out.println(word.length());
        return (len1-len2)/word.length();
    }
    public static HashSet<String> ReadWord(String dir){
        HashSet<String> set = new HashSet<String>();
        BufferedReader bfr = null;
        try {
            bfr = new BufferedReader(new FileReader(new File(dir)));
            String s;
            while ((s = bfr.readLine())!=null){
                if(s.trim().length()>0&&s.length()>1){
                    set.add(s);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bfr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return set;
    }
}
